package com.inspur.edp.common.expr.visitor.array;

import com.inspur.edp.common.expr.ast.ASTContext;
import com.inspur.edp.common.expr.ast.CompileUnitParser;
import com.inspur.edp.common.expr.ast.CompileUnitVisitor;
import com.inspur.edp.common.expr.spi.ArrayAccessVisitor;
import com.inspur.edp.common.expr.visitor.JavaVisitor;
import com.inspur.edp.common.expr.visitor.VisitResult;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/9/12 11:03
 */
public class ArrayListAccessVisitor implements ArrayAccessVisitor {

    public static final String KIND = "ArrayList";

    @Override
    public String getKind() {
        return KIND;
    }

    @Override
    public VisitResult visitGetter(CompileUnitVisitor visitor, ASTContext context, CompileUnitParser.ExpressContext objectExpr, CompileUnitParser.ExpressContext indexExpress) {
        JavaVisitor javaVisitor = (JavaVisitor) visitor;
        String code = javaVisitor.visitExpress(objectExpr) +
                ".get(" + javaVisitor.visitExpress(indexExpress) + ")";
        return new VisitResult(code);
    }

    @Override
    public VisitResult visitSetter(CompileUnitVisitor visitor,
                                   ASTContext context,
                                   CompileUnitParser.ExpressContext objectExpr,
                                   CompileUnitParser.ExpressContext indexExpress,
                                   CompileUnitParser.ExpressContext valueExpress) {
        JavaVisitor javaVisitor = (JavaVisitor) visitor;
        String code = javaVisitor.visitExpress(objectExpr) +
                "." +
                "set(" + javaVisitor.visitExpress(indexExpress) + "," + javaVisitor.visitExpress(valueExpress) + ")";
        return new VisitResult(code);
    }
}
